// +----------------------------------------------------------------------
// | JavaWeb_Vue_Pro前后端分离旗舰版框架 [ JavaWeb ]
// +----------------------------------------------------------------------
// | 版权所有 2019~2020 南京JavaWeb研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.javaweb.vip/
// +----------------------------------------------------------------------
// | 作者: 鲲鹏 <1175401194@qq.com>
// +----------------------------------------------------------------------

package com.javaweb.system.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.utils.CommonUtils;
import com.javaweb.system.common.BaseQuery;
import com.javaweb.system.common.BaseServiceImpl;
import com.javaweb.system.constant.CmsChannelConstant;
import com.javaweb.system.entity.CmsChannel;
import com.javaweb.system.mapper.CmsChannelMapper;
import com.javaweb.system.query.CmsChannelQuery;
import com.javaweb.system.service.ICmsChannelService;
import com.javaweb.system.utils.ShiroUtils;
import com.javaweb.system.vo.cmschannel.CmsChannelInfoVo;
import com.javaweb.system.vo.cmschannel.CmsChannelListVo;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.*;

/**
  * <p>
  * 栏目表 服务类实现
  * </p>
  *
  * @author 鲲鹏
  * @since 2021-06-04
  */
@Service
public class CmsChannelServiceImpl extends BaseServiceImpl<CmsChannelMapper, CmsChannel> implements ICmsChannelService {

    @Autowired
    private CmsChannelMapper cmsChannelMapper;

    /**
     * 获取数据列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public JsonResult getList(BaseQuery query) {
        CmsChannelQuery cmsChannelQuery = (CmsChannelQuery) query;
        // 查询条件
        QueryWrapper<CmsChannel> queryWrapper = new QueryWrapper<>();
        // 类型：1channel  2page 3link 4list
        if (!StringUtils.isEmpty(cmsChannelQuery.getType())) {
            queryWrapper.eq("type", cmsChannelQuery.getType());
        }
        // 名称
        if (!StringUtils.isEmpty(cmsChannelQuery.getName())) {
            queryWrapper.like("name", cmsChannelQuery.getName());
        }
        // 状态: 1normal 2hidden
        if (!StringUtils.isEmpty(cmsChannelQuery.getStatus())) {
            queryWrapper.eq("status", cmsChannelQuery.getStatus());
        }
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByDesc("id");

        // 获取数据列表
        IPage<CmsChannel> page = new Page<>(cmsChannelQuery.getPage(), cmsChannelQuery.getLimit());
        IPage<CmsChannel> pageData = cmsChannelMapper.selectPage(page, queryWrapper);
        pageData.convert(x -> {
            CmsChannelListVo cmsChannelListVo = Convert.convert(CmsChannelListVo.class, x);
            // 类型描述
            if (cmsChannelListVo.getType() != null && cmsChannelListVo.getType() > 0) {
                cmsChannelListVo.setTypeName(CmsChannelConstant.CMSCHANNEL_TYPE_LIST.get(cmsChannelListVo.getType()));
            }
            // 图片地址
            if (!StringUtils.isEmpty(cmsChannelListVo.getImage())) {
                cmsChannelListVo.setImage(CommonUtils.getImageURL(cmsChannelListVo.getImage()));
            }
            // 标志描述
            if (cmsChannelListVo.getFlag() != null && cmsChannelListVo.getFlag() > 0) {
                cmsChannelListVo.setFlagName(CmsChannelConstant.CMSCHANNEL_FLAG_LIST.get(cmsChannelListVo.getFlag()));
            }
            // 状态描述
            if (cmsChannelListVo.getStatus() != null && cmsChannelListVo.getStatus() > 0) {
                cmsChannelListVo.setStatusName(CmsChannelConstant.CMSCHANNEL_STATUS_LIST.get(cmsChannelListVo.getStatus()));
            }
            return cmsChannelListVo;
        });
        return JsonResult.success(pageData);
    }

    /**
     * 获取详情Vo
     *
     * @param id 记录ID
     * @return
     */
    @Override
    public Object getInfo(Serializable id) {
        CmsChannel entity = (CmsChannel) super.getInfo(id);
        // 返回视图Vo
        CmsChannelInfoVo cmsChannelInfoVo = new CmsChannelInfoVo();
        // 拷贝属性
        BeanUtils.copyProperties(entity, cmsChannelInfoVo);
        // 图片
        if (!StringUtils.isEmpty(cmsChannelInfoVo.getImage())) {
            cmsChannelInfoVo.setImage(CommonUtils.getImageURL(cmsChannelInfoVo.getImage()));
        }
        return cmsChannelInfoVo;
    }

    /**
     * 添加、更新记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult edit(CmsChannel entity) {
        // 图片
        if (entity.getImage().contains(CommonConfig.imageURL)) {
            entity.setImage(entity.getImage().replaceAll(CommonConfig.imageURL, ""));
        }
        if (entity.getId() != null && entity.getId() > 0) {
            entity.setUpdateTime(DateUtils.now());
            entity.setUpdateUser(1);
        } else {
            entity.setCreateTime(DateUtils.now());
            entity.setCreateUser(1);
        }
        return super.edit(entity);
    }

    /**
     * 删除记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult delete(CmsChannel entity) {
        entity.setUpdateTime(DateUtils.now());
        entity.setUpdateUser(1);
        entity.setMark(0);
        return super.delete(entity);
    }

    /**
     * 设置状态
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult setStatus(CmsChannel entity) {
        if (entity.getId() == null || entity.getId() <= 0) {
            return JsonResult.error("记录ID不能为空");
        }
        if (entity.getStatus() == null) {
            return JsonResult.error("记录状态不能为空");
        }
        return super.setStatus(entity);
    }

}